package mbp.gui.panels;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import mbp.core.ResourceUtility;
import mbp.core.Service;
import mbp.gui.base.IListPanel;
import mbp.message.service;
import mbp.message.utility;

public class ServiceListPanel extends JPanel implements IListPanel
{

	private JTable SrvTable;
	private DefaultTableModel SrvModel;
	
	private List<service> srviList;
	private List<Service> srvList;
	
	/**
	 * Create the panel.
	 */
	public ServiceListPanel() 
	{
		setLayout(new BorderLayout(0, 0));
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);
		
		SrvModel = new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"Index", "Type", "Realize time"
				}
			);
		
		SrvTable = new JTable(SrvModel){
			@Override
			public boolean isCellEditable(int row, int column) {
		    // set table column uneditable
		    return false;
			}
		};
		
		SrvTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		SrvTable.setFillsViewportHeight(true);
		SrvTable.setToolTipText("Double click for Servise details");
		
		scrollPane.setViewportView(SrvTable);
		  
		//podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		SrvTable.addMouseListener(new MouseAdapter(){
		     public void mouseClicked(MouseEvent e){
		      if (e.getClickCount() == 2){
		         processDoubleClick(SrvTable.getSelectedRow());
		         }
		      }
		     } );

	}
	
	public void processDoubleClick(int index)
	{
		System.out.println(" double click: selected row: " + SrvTable.getSelectedRow());
	}
	
	public JTable getTable()
	{
		return SrvTable;
	}
	

	
	private void reloadInfo()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				int rows = 0;
				do
				{
					rows = SrvModel.getRowCount();
					if(rows>0)
					{
						SrvModel.removeRow(0);
					}
				}while(rows>0);
				
				for(int i = 0; i<srviList.size(); i++)
				{
					String index = Integer.toString(i);
					String type = srviList.get(i).type;
					String time = srviList.get(i).realizeTerm;
					SrvModel.insertRow(SrvModel.getRowCount(), new Object[]{index, type, time});
				}
				
			}
		});
	}
	
	private void reloadObj()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				int rows = 0;
				do
				{
					rows = SrvModel.getRowCount();
					if(rows>0)
					{
						SrvModel.removeRow(0);
					}
				}while(rows>0);
				
				for(int i = 0; i<srvList.size(); i++)
				{
					String index = Integer.toString(i);
					String type = srvList.get(i).getType().getServiceTypeName();
					String time = srvList.get(i).getServiceRealizeTerm().toString();
					SrvModel.insertRow(SrvModel.getRowCount(), new Object[]{index, type, time});
				}
				
			}
		});
	}

	@Override
	public <service> void reloadListInfo(List<service> infoList) {
		this.srviList = (List<mbp.message.service>) infoList;
		this.srvList = null;
		reloadInfo();
		
	}

	@Override
	public <Service> void reloadListObj(List<Service> objList) {
		this.srvList = (List<mbp.core.Service>) objList;
		this.srviList = null;
		reloadObj();
		
	}

	@Override
	public void reloadList() {
		if(srvList != null)
		{
			reloadObj();
		}
		if(srviList != null)
		{
			reloadInfo();
		}
		
	}

}
